<?php
/**
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 *
 */

class html 
{
	/**
	 * 存储页头，相同项会覆盖
	 */
	static private $_head = array();
	
	static function pushHead($str, $isFR=0)
	{
		foreach(explode('||', $str) as $str)
		 {
			if ($isFR)
				$str = ROOT_URL . STYLE_PATH_F .$str;
			self::$_head[$str] =$str;
		 }
			
	}
	static function showHead()
	{
		foreach (self::$_head as $str)
		 {
		 	$_a = explode('.', $str);
			$iu = preg_match("/^http:|^https:/",$str)>0;
			$str = str_replace('"', '', $str);
			switch (array_pop($_a) )
		 	{
            	case 'css':
                  $str = '<link rel="stylesheet" type="text/css" href="'. ($iu ?$str :ROOT_URL.STYLE_PATH_A.$str) .'" />';
                  break;
				case 'js':
                  $str = '<script type="text/javascript" src="'. ($iu ?$str :ROOT_URL.JS_PATH.$str) .'"></script>';
                  break;
         	}
		 	echo $str;
		 }
        self::$_head = array();
	}
    
    // 引用助手
    static function helper($m)
    {
        $name = '\h\\'. $m;
        return new $name();
    }


    // form
	static function selectR($att, $arr, $selVal='', $fistOpt='', $vKey=null, $nKey=null)
	{
		echo '<select '. $att .'>';
		self::select_optR($arr, $selVal, $fistOpt, $vKey, $nKey);
        echo '</select>';	
	}
	static function select_optR($arr, $selVal='', $fistOpt='', $vKey=null, $nKey=null)
	{  
		$vKey || $vKey = 'id';
		$nKey || $nKey = 'name';
		if(isset($fistOpt))
		 {
			if(is_string($fistOpt))
				$fistOpt = array($nKey=>$fistOpt);
			echo '<option value="'. $fistOpt[$vKey] .'">'. htmlspecialchars($fistOpt[$nKey]) .'</option>';
		 }
		 
        if(!is_array($selVal))
            $selVal = explode(',', $selVal);
		foreach($arr as $v)
        	echo '<option value="'. $v[$vKey] .'" '. ( in_array($v[$vKey], $selVal) ?'selected="selected"':'' ) .'>'. htmlspecialchars($v[$nKey]) .'</option>';
	}
	static function select($att, $arr, $selVal='', $fistOpt='')
	{
		echo '<select '. $att .'>';
		if (isset($fistOpt))
			echo '<option value="">'. $fistOpt .'</option>';
		self::select_opt($arr, $selVal);
        echo '</select>';	
	}
	static function select_opt($arr, $selVal='')
	{  
		foreach($arr as $k=>$v)
        	echo '<option value="'. $k .'" '. ("$k"=="$selVal"?'selected="selected"':'') .'>'. htmlspecialchars($v) .'</option>';
	}
	static function selectName($att, $arr, $selVal='', $fistOpt='')
	{
		echo '<select '. $att .'>';
		if($fistOpt)
			echo '<option value="">'. $fistOpt .'</option>';
		foreach($arr as $v)
        	echo '<option value="'. htmlq($v) .'" '. ("$v"=="$selVal"?'selected="selected"':'') .'>'. htmlspecialchars($v).'</option>';
        echo '</select>';
	}
	static function radioR($att, $arr, $val='', $vKey=null, $nKey=null, $el='&nbsp;&nbsp;')
	{
        isset($vKey) || $vKey = 'id';
		isset($nKey) || $nKey = 'name';
		foreach($arr as $v)
			echo '<label><input type="radio" value="'.$v[$vKey].'"  '.$att . ($v[$vKey]=="$val"?' checked="checked"':'') . ' />'.htmlspecialchars($v[$nKey]).'</label>'. $el;
	}
	static function radio($att, $arr, $val='')
	{
		foreach($arr as $k=>$v)
			echo '<label><input type="radio" value="'.$k.'"  '.$att . ($k=="$val"?' checked="checked"':'') . ' />'.$v.'</label> &nbsp;';
	}
	static function radioName($att, $arr, $val='')
	{
		foreach($arr as $v)
			echo '<label><input type="radio" value="'.htmlq($v).'"  '.$att . ($v=="$val"?' checked="checked"':'') . ' />'.htmlspecialchars($v).'</label> &nbsp;';
	}
	static function checkbox($att, $arr, $val=array(), $el=' ')
	{
        if(!is_array($val))
            $val = explode(',', $val);
		foreach($arr as $k=>$v)
			echo '<label><input type="checkbox" value="'.$k.'"  '.$att . (in_array($k, $val)?' checked="checked"':'') . ' />'.htmlspecialchars($v).'</label>'.$el;
	}
	static function checkboxName($att, $arr, $val=array())
	{
		if(!is_array($val))
            $val = explode(',', $val);
		foreach($arr as $v)
			echo '<label><input type="checkbox" value="'.$v.'"  '.$att . (in_array($v, $val)?' checked="checked"':'') . ' />'.$v.'</label> ';
	}
	static function checkboxR($att, $arr, $val=array(), $vKey=null, $nKey=null, $el=' ')
	{
		isset($vKey) || $vKey = 'id';
		isset($nKey) || $nKey = 'name';
		if( !is_array($val))
			$val = explode(',', $val);
		foreach($arr as $v)
			echo '<label><input type="checkbox" value="'.$v[$vKey].'"  '.$att . (in_array($v[$vKey], $val)?' checked="checked"':'') . ' />'.htmlspecialchars($v[$nKey]).'</label>'.$el;
	}
}
